package com.youhui.weixin.common.exception;

import com.youhui.weixin.common.info.NoticeInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by https://github.com/kuangcp on 17-6-12  上午9:57
 * 暂时使用时间来记录，考虑是否添加时间日期上去
 */
public class MythException extends Exception{
    private static Logger logger;
    public MythException() {}

    public MythException(ExceptionMap message) {
        super(message.toString());
    }

    public MythException(ExceptionMap message, Class location) {
        super(message.toString());
        init(location);
        logger.error(" "+this.getClass().getSimpleName()+" : "+message);
    }
    public MythException(ExceptionMap message,Throwable cause, Class location) {
        super(message.toString(),cause);
        init(location);
        logger.error(" "+this.getClass().getSimpleName()+" :"+message);
        logger.debug(NoticeInfo.ERROR_INFO,cause);
    }

    /**
     * 记录异常不上抛，一些特别的地方需要，例如finally块
     */
    public static void recode(ExceptionMap message,Throwable cause, Class location){
        recode(message.toString(), cause, location);
    }
    public static void recode(String message,Throwable cause, Class location){
        logger = LoggerFactory.getLogger(location);
        logger.error(" "+MythException.class.getSimpleName()+" :"+message);
        logger.debug(NoticeInfo.ERROR_INFO,cause);
    }

    private static void init(Class location){
        logger = LoggerFactory.getLogger(location);
    }
    public static void debug(String message, Throwable cause, Class location){
        init(location);
        logger.debug(location.getSimpleName()+":"+message, cause);
    }
    public static void error(String message, Throwable cause, Class location){
        init(location);
        logger.error(location.getSimpleName()+":"+message+cause.getMessage());
    }
}
